package com.JavaNet.chapter8_client_socket.half_quplex;


import java.net.*;
import java.io.*;

/**
 * @author : wushikai
 * <p>
 * date : 2021-04-20
 */
@Deprecated
public class MyServer2 {

    public static void main(String[] args) {
        MyServer2 ms2= new MyServer2();
    }

    public MyServer2() {
        try {
            //在9999端口监听
            System.out.println("服务器正在9999端口监听....");
            ServerSocket ss= new ServerSocket(9999);

            //等待连接
            Socket socket= ss.accept();

            //先接收客户端发来的信息
            InputStreamReader isr= new InputStreamReader(socket.getInputStream());
            BufferedReader br= new BufferedReader(isr);

            PrintWriter pw= new PrintWriter(socket.getOutputStream(),true);
            BufferedWriter bf  = new BufferedWriter(pw);


            //接收从控制台输入的信息
            InputStreamReader isr2= new InputStreamReader(System.in);
            BufferedReader br2= new BufferedReader(isr2);

            while(true){

                String msg= br.readLine();

                System.out.print("客户端发来："+ msg);
                //退出对话
                if(msg.equals("bye")){
                    System.out.println("对话结束！");
                    socket.close();
                    break;
                }
                //接受从控制台输入的信息
                System.out.println("输入你希望对客户端说的话：");
                String info= br2.readLine();

                //把从控制台接受的信息回送给客户端
                pw.print(info);
                bf.write(info);
                bf.flush();
            }

            socket.shutdownInput();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
